/**
 * 下载中心管理
 * @authors supmain (mtingfeng@gmail.com)
 * @date    2015-01-28 11:40:12
 * @for 下载中心管理.html
 * @version $1.0$
 */
var BSTMG = BSTMG || {};
BSTMG.downloadManager = {
	init: function(){
		var that = this;
		/* 全选 */
		that.checkAll();
		/* 新增 */
		that.operateManager();
	},
	checkAll: function(){
		var $table = $("#downloadTable"),
			$thead = $table.find("thead"),
			$tbody = $table.find("tbody");
		// 全选
		$thead.on("click",".simple",function(){
			var $this = $(this),
				$parent = $this.parents('table.table'),
				$checkbox = $parent.find('input[type=checkbox]').not($this);
			$this.prop("checked") ? $checkbox.prop("checked",true) : $checkbox.prop("checked",false) ;
		});
		$tbody.on("click","input[type=checkbox]",function(){
			var $this = $(this),
				$all = $this.parents("tbody").eq(0).find("input[type=checkbox]"),
				$checkbox = $all.not($this).filter(":checked");
			var $checkall = $this.parents('table.table').find("thead input.simple");
			( $this.prop("checked") && ($checkbox.length + 1) == $all.length ) ? $checkall.prop("checked",true) : $checkall.prop("checked",false) ;
		});
	},
	operateManager: function(){
		var $table = $("#downloadTable"),
			$thead = $table.find("thead"),
			$tbody = $table.find("tbody");
		var $operator = $(".operator");
		$operator.on("click",".delete",function(){
			var array = [];
			$tbody.find("input[type=checkbox]:checked").each(function(){
				var $this = $(this),
					id = $.trim( $this.attr( "data-id" ) );
				array.push( id );
			});
			if( array.length ){
				BSTYUN.dialog.confirm("确定要删除吗？",function(){
					deleteDownload( array );
				});
			}else{
				BSTYUN.dialog.warn("请选择一条数据！");
			};
		})
		$tbody.on("click",".delete",function(){
			var $this = $(this),
				$checkbox = $this.parent().parent().find("td:first-child input"),
				id = $.trim( $checkbox.attr("data-id") );
			BSTYUN.dialog.confirm("确定要删除吗？",function(){
				deleteDownload( [id] );
			});
		});
		// 删除用户
		function deleteDownload( array ){
			$.ajax({
 				url: '/sysAdmin/operate/downAdmin/deleteById',
 				type: 'post',
 				dataType: 'json',
 				data: {
 					ids:array.join(",") 
 				},
 				beforeSend: function(){},
 				complete: function(){},
 				success: function( data ){
 					$tbody.find("input[type=checkbox]").each(function(){
 						var $this = $(this),
 							id = $.trim( $this.attr( "data-id" ) );
 						if( array.indexOf( id ) >= 0 ){
 							$this.parent().parent().remove();
 						};
 					});
 					location.href = "/sysAdmin/downAdmin/downManager";
 				},
 				error: function(error){
 					BSTYUN.dialog.warn("网络异常，请联系管理员！");
 				}
 			});
		};
	}
};
$(function(){
	BSTMG.downloadManager.init();
});